package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-08-16
 * Time: 22:14
 */
//创建一个枚举类
public enum TestEnum {//默认继承于枚举类Enum
    //枚举对象,逗号分开
    RED(0,"红色"),
    GREEN(1 , "绿色"),
    BLACK(2,"黑色");

    //枚举的构造方法默认是私有的，所以里面的构造方法写不写private都行，默认不写
    private int ordinal;
    private String color;
    TestEnum(int ordinal , String color){
        this.ordinal = ordinal;
        this.color = color;
    }//重点TestEnum 继承 Enum类，默认编译器会调用构造方法


    public static void main2(String[] args) {
        TestEnum[] testEnums = TestEnum.values(); //将枚举的成员以数组的方式返回
        for (int i = 0; i < testEnums.length; i++) {
            System.out.println(testEnums[i].ordinal()  );//ordinal就是获取枚举成员的索引位置，可以理解成数组下标
        }
        System.out.println("=======");

        TestEnum v = TestEnum.valueOf("pink");
        System.out.println(v);

        System.out.println("======");
        System.out.println(RED.compareTo(BLACK));//输出-2，按定义顺序比较
    }

    public static void main(String[] args) {
        TestEnum color = RED;

        switch (color){
            case RED:
                System.out.println(RED);
                break;
            case BLACK:
                System.out.println(BLACK);
                break;
            case GREEN:
                System.out.println(GREEN);
                break;
            default:
                System.out.println("error");
                break;
        }
    }
}
